package com.xf.protocol;



import com.xf.rigister.RemoteRigister;

import java.io.*;
import java.net.*;
import java.util.concurrent.*;

public class HttpServer {
    private ServerSocket serverSocket_pro;
    private ServerSocket serverSocket_con;

    public void start_for_rigister(String Address,Integer port1,Integer port2) throws IOException, InterruptedException {
        InetAddress address = InetAddress.getByName(Address);
        serverSocket_pro = new ServerSocket(port1,50,address);
        serverSocket_con = new ServerSocket(port2,50,address);
        System.out.println("注册中心ip地址及端口:\n"+Address+":"+port1+"/"+port2);

        //从本地读取已经注册过的服务
        RemoteRigister.loadinfo();

        //定期保存服务信息在本地，防止注册中心崩溃
        ScheduledExecutorService scheduler2 = Executors.newScheduledThreadPool(1);
        scheduler2.scheduleAtFixedRate(RemoteRigister::saveinfo, 0, 20, TimeUnit.SECONDS);



        int corePoolSize = 5;
        int maximumPoolSize = 5;
        long keepAliveTime = 60L;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue1 = new LinkedBlockingQueue<>(100);
        BlockingQueue<Runnable> workQueue2 = new LinkedBlockingQueue<>(100);

        ThreadPoolExecutor executor1 = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue1
        );
        ThreadPoolExecutor executor2 = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue2
        );
        executor1.prestartAllCoreThreads();
        executor2.prestartAllCoreThreads();

        while (true){
            try{
                Runnable task_pro = new HttpServerHandler_pro_rig(serverSocket_pro);
                executor1.execute(task_pro);
                Runnable task_con = new HttpServerHandler_con_rig(serverSocket_con);
                executor2.execute(task_con);
            }
            catch (RejectedExecutionException e) {
            }
        }
    }

    public void start_for_provider(String RigisterAddress,Integer port1,String ServerAddress,Integer port2) throws IOException {
        InetAddress address = InetAddress.getByName(ServerAddress);
        serverSocket_con = new ServerSocket(port2,50,address);
        System.out.println("服务端ip地址及端口:\n"+ServerAddress+":"+port2);




        int corePoolSize = 5;
        int maximumPoolSize = 5;
        long keepAliveTime = 60L;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(100);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue
        );
        executor.prestartAllCoreThreads();
        while (true){
            try{
                Runnable task_con = new HttpServerHandler_rig_pro(serverSocket_con);
                executor.execute(task_con);
                Thread.sleep(1000);
            }
            catch (RejectedExecutionException e) {
            }
            catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

}
